#include <errno.h>
#include <stdio.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define SERVER_ADDR "127.0.0.1"
#define SERVER_PORT 8080
#define BUF_SIZE    1024

int main(int argc, char **argv)
{
	int ret = 0;
	char info[BUF_SIZE] = {0};
	
	int sock_cln = 0;
	struct sockaddr_in sockaddr_svr = {0};

	/* create socket */
	sock_cln = socket(AF_INET, SOCK_STREAM, 0);
	if( sock_cln < 0 )
	{
		printf( "create sock error!\n" );
		return -1;
	}
	
	/* sockaddr */
	sockaddr_svr.sin_family = AF_INET;
	sockaddr_svr.sin_port = htons(SERVER_PORT);
	sockaddr_svr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
	
	/* connect */
	ret = connect( sock_cln, (struct sockaddr*)&sockaddr_svr, sizeof(sockaddr_svr) );
	if( -1 == ret )
	{
		printf( "connect: %d\n", ret );
		return -1;
	}
	
	while( 1 )
	{
		/* send */
		ret = send( sock_cln, "hello", sizeof("hello"), 0 );
		printf( "sent: %d\n", ret );
		
		/* receive */
		ret = recv( sock_cln, info, BUF_SIZE, 0 );
		printf( "recv: %d\n", ret );
	}
	
	close( sock_cln );
	
	printf( "closed...\n" );
	return 0;
}
